package com.jinlejia.bbs.ui.fun;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.InvitedUserAdapter;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.databinding.BbsActivityInvitedUserListBinding;
import com.jinlejia.bbs.help.CommonEvent;
import com.jinlejia.bbs.logic.InvitedUserListLogic;
import com.jinlejia.bbs.widget.recycler.PullInterface;

import java.util.ArrayList;

public class BBSInvitedUserListActivity extends AppCompatActivity implements InvitedUserListLogic.LogicCallBack, PullInterface.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    private BbsActivityInvitedUserListBinding binding;
    private String tags;
    private InvitedUserListLogic logic;
    private ArrayList<BBSUserBean> dataList;
    private String topicCode;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        binding = DataBindingUtil.setContentView(this,R.layout.bbs_activity_invited_user_list);
        binding.setCommonEvent(CommonEvent.obtain());
        topicCode = getIntent().getStringExtra("topicCode");
        tags = getIntent().getStringExtra("tag");
        dataList=new ArrayList<>();
        logic = new InvitedUserListLogic(this,dataList,this);
        initView();
    }
    private InvitedUserAdapter adapter;
    private void initView() {
        adapter = new InvitedUserAdapter(this,topicCode);
        adapter.setInvitedResultListener(new InvitedUserAdapter.InvitedResultListener() {
            @Override
            public void onResult(BBSUserBean result,String topicCode) {
                Intent intent = new Intent();
                intent.setClass(context,BBSInvitedViewListActivity.class);
                intent.putExtra("user",result);
                intent.putExtra("topicCode",topicCode);
                startActivity(intent);
            }
        });
        binding.recyclerView.setHasFixedSize(true);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setAutoLoadMoreListener(this);
        binding.swipeRefreshLayout.setOnRefreshListener(this);
        binding.swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                binding.swipeRefreshLayout.setRefreshing(true);
                logic.refresh(tags);
            }
        });
    }

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        topicCode = intent.getStringExtra("topicCode");
        tags = intent.getStringExtra("tag");
        dataList=new ArrayList<>();
        logic = new InvitedUserListLogic(this,dataList,this);
        setIntent(intent);
        initView();
    }

    @Override
    public void onResult(boolean hasNextPage) {
        adapter.initData(dataList);
        if (!hasNextPage){
            binding.recyclerView.setAutoLoadMoreListener(null);
        }else {
            binding.recyclerView.setAutoLoadMoreListener(this);
        }
        binding.swipeRefreshLayout.setRefreshing(false);
        binding.recyclerView.getAdapter().notifyDataSetChanged();
    }


    @Override
    public void onLoadMore() {
        logic.load(tags);
    }

    @Override
    public void onRefresh() {
        logic.refresh(tags);
    }
}
